package cn.itcast.web.servlet;

import cn.itcast.domain.Province;
import cn.itcast.service.ProvinceService;
import cn.itcast.service.impl.ProvinceServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //1.调用service查询
        ProvinceService provinceService=new ProvinceServiceImpl();
        List<Province> list = provinceService.findAll();
        //2.序列化list为json
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);

       /* ProvinceService provinceService = new ProvinceServiceImpl();
        String json = provinceService.findAllJson();*/

        System.out.println(json);

        //3.响应结果
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
